// LEC				March 26, 2021
// 
//////////////////////////////////////////////////////////////////////////////////
// STATE SIZE PROJECT
// Generic Data Prep File (Part 1)
// for CShapes, Briding data, etc.
////////////////////////////////////////////////////////////////////////////////// 




////////////////////////////////////////////////////////////////////////////////// 
// Creates metropolitan-level file: metvars  (not used for core results)
// This file covers the sovereign states or the metropolitan cores of empires
//////////////////////////////////////////////////////////////////////////////////

cd $INDIR 
insheet using $STATEFILE, clear


// State is dependent territory
gen depend = 0
replace depend = 1 if holder!=id & holder!=.

bys id: egen depanytime = max(depend)

duplicates drop id year, force
xtset id year

// State just became independent
gen liberated = 0
replace liberated = 1 if depend==1 & f.depend==0

// Secession flag at the level of dependencies
bys holder year: egen depsec = sum(liberated)
gen depsec1 = 0
replace depsec1 = 1 if depsec>0 & depsec!=.

// Compute stats including dependencies
// empire: state has dependencies
// ndep: number of dependencies
// sarea: entire area includin both core and dependencies
bys holder year: egen empire = max(depend)
bys holder year: egen ndep = sum(depend)
bys holder year: egen sarea = sum(area)

// From now on, get rid of the dependencies
drop if holder != id
drop holder  // use state id var as the main index

// calculate metropolis-specific vars
gen metarea = area
gen sharemet = area/sarea
drop area

sort id year



sort id year

cd $OUTDIR
save metvars, replace



////////////////////////////////////////////////////////////////////////////////// 
// Creates state-level file: statvars  (essential)
// This file holds state-level data of aggregate state units including dependencies
//////////////////////////////////////////////////////////////////////////////////

cd $INDIRAGG
insheet using $AGGSTATEFILE, clear

xtset id year
sort id year

// Merge in metvars (see above)
cd $OUTDIR
merge id year using metvars
drop _merge



// Continuity id coding
// idc is the extended id code that offers continuity based corrections to follow
gen idc = id

// Here are the explicit corrections that guarantee continuity of governance
if $IDCONT {


gen i = id if name == "Saxe-Gotha"  
egen itemp = max(i)
replace idc = itemp if name == "Saxe-Coburg-Gotha"
drop if idc == itemp & area == .
drop i itemp
/*

gen i = id if name == "Italy"  
egen itemp = max(i)
replace idc = itemp if name == "Piedmont"
drop if idc == itemp & area == .
drop i itemp

NAME CHANGE ALREADY IMPLEMENTED
gen i = id if name == "Austria"  
egen itemp = max(i)
replace idc = itemp if name == "Habsburgs"
replace idc = itemp if name == "Austria-Hungary"
drop if idc == itemp & area == .
drop i itemp
*/

gen i = id if name == "Germany"  
egen itemp = max(i)
// replace idc = itemp if name == "Prussia" // Name change already implemented
replace idc = itemp if name == "West Germany"
drop if idc == itemp & area == .
drop i itemp

/*
gen i = id if name == "Turkey"  
egen itemp = max(i)
replace idc = itemp if name == "Ottomans"
drop if idc == itemp & area == .
drop i itemp
*/

gen i = id if name == "Russia"  
egen itemp = max(i)
replace idc = itemp if name == "Soviets"
replace idc = itemp if name == "Russia"
drop if idc == itemp & area == .
drop i itemp


gen i = id if name == "Yugoslavia"  
egen itemp = max(i)
replace idc = itemp if name == "Serbia"
drop if idc == itemp & area == .
drop i itemp


gen i = id if name == "Czechoslovakia"  
egen itemp = max(i)
replace idc = itemp if name == "Czech Republic"
drop if idc == itemp & area == .
drop i itemp


}

// Now compute state births and deaths
bys idc: egen maxyear = max(year) if area!=.
bys idc: egen minyear = min(year) if area!=.

sort idc year
xtset idc year

gen birth = 0
replace birth = 1 if area!=. & l.area==. & year>$STARTYEAR
gen firstbirth = 0
replace firstbirth = 1 if birth==1 & year==minyear
gen death = 0
replace death = 1 if l.area!=. & area==. & year<$ENDYEAR
gen finaldeath = 0
replace finaldeath = 1 if death == 1 & year == maxyear+1

egen name_id = group(name)
gen namechange = 0
replace namechange = 1 if l.name_id!=name_id & l.name_id!=. & year>$STARTYEAR

sort id year
cd $OUTDIR
save statevars, replace





